package javaweb.web;

import javaweb.pojo.User;
import javaweb.servlet.impl.UserServletImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author shkstart
 * @creat ${YEAR-}-07-14 19:53
 */
public class RegistServlet extends HttpServlet {
    UserServletImpl userServlet= new UserServletImpl();
    protected void doRegistPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //  1、获取请求的参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String code = request.getParameter("code");
//        2、检查 验证码是否正确  === 写死,要求验证码为:abcde
        if ("abcde".equalsIgnoreCase(code)) {
//        3、检查 用户名是否可用
            if (userServlet.checkUserName(username)) {
               // System.out.println("用户名[" + username + "]已存在!");

                // 把回显信息，保存到requstuest域中
                request.setAttribute("msg", "用户名已存在！！");
                request.setAttribute("username", username);
                request.setAttribute("email", email);

//        跳回注册页面
                request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);
            } else {
                //      可用
//                调用Sservice保存到数据库
                userServlet.registUser(new User( username, password, email));
//
//        跳到注册成功页面 regist_success_menu.jsp
                request.getRequestDispatcher("/pages/user/regist_success_menu.jsp").forward(request, response);
            }
        } else {
            // 把回显信息，保存到requstuest域中
            request.setAttribute("msg", "验证码错误！！");
            request.setAttribute("username", username);
            request.setAttribute("email", email);

            System.out.println("验证码[" + code + "]错误");
            request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);
        }
    }
    }



